package com.baizhi.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController
public class CodeController {

    @RequestMapping("getCodeImg")
    public void getCodeImg(HttpServletResponse response , HttpSession session) throws Exception {
        //生成验证码
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(120, 35, 4, 20);

        //获取相应对象的输出流
        ServletOutputStream stream = response.getOutputStream();

        //把图片放入流中
        lineCaptcha.write(stream);

        //将验证码的信息树村在session中
        session.setAttribute("codeData" , lineCaptcha.getCode());

        //关流
        stream.close();

    }

}
